<template>
  <div>
    <el-form ref="form" label-width="80px" :model="spu">
      <el-form-item label="SPU名称">
        <el-input placeholder="SPU名称" v-model="spu.spuName"></el-input>
      </el-form-item>
      <el-form-item label="品牌">
        <el-select placeholder="请选择品牌" v-model="spu.tmId">
          <el-option
            :label="tm.tmName"
            :value="tm.id"
            v-for="tm in trademark"
            :key="tm.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="SPU描述">
        <el-input
          type="textarea"
          rows="4"
          placeholder="SPU描述"
          v-model="spu.description"
        ></el-input>
      </el-form-item>
      <el-form-item label="SPU图片">
        <el-upload
          action="/dev-api/admin/product/fileUpload"
          list-type="picture-card"
          :file-list="ImageUrl"
          :on-preview="handlePictureCardPreview"
          :on-remove="handleRemove"
          :on-success="handelerImg"
        >
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="销售属性">
        <el-select
          :placeholder="`还有${unselectArrList.length}项未选择`"
          v-model="attrId"
        >
          <el-option
            :label="xm.name"
            :value="`${xm.id}:${xm.name}`"
            v-for="xm in unselectArrList"
            :key="xm.id"
          ></el-option>
        </el-select>
        <el-button
          type="primary"
          icon="el-icon-plus"
          :disabled="!attrId"
          @click="AddSaleAttrList()"
          >添加销售属性</el-button
        >
        <el-table style="width: 100%" border :data="spu.spuSaleAttrList">
          <el-table-column
            type="index"
            label="序号"
            width="80px"
            align="center"
          >
          </el-table-column>
          <el-table-column prop="saleAttrName" label="属性名" width="width">
          </el-table-column>
          <el-table-column prop="prop" label="属性名称列表" width="width">
            <template slot-scope="{ row }">
              <!-- @close="handleClose(tag)" -->
              <el-tag
                style="margin-left: 10px"
                :key="tag.id"
                v-for="(tag, index) in row.spuSaleAttrValueList"
                closable
                :disable-transitions="false"
                @close="row.spuSaleAttrValueList.splice(index, 1)"
              >
                {{ tag.saleAttrValueName }}
              </el-tag>
              <el-input
                class="input-new-tag"
                v-if="row.inputVisible"
                v-model="row.inputValue"
                ref="saveTagInput"
                size="small"
                @keyup.enter.native="handleInputConfirm(row)"
                @blur="handleInputConfirm(row)"
              >
              </el-input>
              <el-button
                style="margin-left: 10px"
                v-else
                class="button-new-tag"
                size="small"
                @click="showInput(row)"
                >添加</el-button
              >
            </template>
          </el-table-column>
          <el-table-column prop="prop" label="操作" width="width">
            <template slot-scope="{ $index }">
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                @click="spu.spuSaleAttrList.splice($index, 1)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="AddOrUpdateSpu">确定</el-button>
        <el-button @click="changeScene">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dialogImageUrl: "",
      dialogVisible: false,
      spu: {
        //商品3id
        category3Id: 0,
        //商品的描述
        description: "",
        //商品的名称
        spuName: "",
        //图片的信息
        spuImageList: [
          {
            id: 0,
            imgName: "",
            imgUrl: "",
            spuId: 0,
          },
        ],
        //商品的下拉列表的属性
        spuSaleAttrList: [
         
        ],
        //tmId
        tmId: "",
      },
      trademark: [],
      ImageUrl: [],
      TypeList: [],
      attrId: "",
    };
  },
  methods: {
    handleRemove(file, fileList) {
      this.ImageUrl = fileList;
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    changeScene() {
      this.$emit("changescene", "0");
      //合并对象，清除数据
      Object.assign(this._data,this.$options.data())
    },
    async inintSpuData(row) {
      //获取商品信息
      let Spureslut = await this.$API.spu.reqSpuInfo(row.id);
      if (Spureslut.code == 200) {
        this.spu = Spureslut.data;
      }
      let tradeMarkListReslut = await this.$API.spu.reqTrademarkList();
      if (tradeMarkListReslut.code == 200) {
        this.trademark = tradeMarkListReslut.data;
      }
      let ImageList = await this.$API.spu.reqSpuImageList(row.id);
      if (ImageList.code == 200) {
        let ArrtList = ImageList.data;
        ArrtList.forEach((item) => {
          item.name = item.imgName;
          item.url = item.imgUrl;
        });
        this.ImageUrl = ArrtList;
      }
      let SpuInfoList = await this.$API.spu.reqBaseAttrList();
      if (SpuInfoList.code == 200) {
        this.TypeList = SpuInfoList.data;
      }
    },
    handelerImg(response, file, fileList) {
      this.ImageUrl = fileList;
    },
    AddSaleAttrList() {
      const IdAndName = this.attrId.split(":");
      const SaleAttrValueList = {
        baseSaleAttrId: IdAndName[0],
        saleAttrName: IdAndName[1],
        spuSaleAttrValueList: [],
      };
      this.spu.spuSaleAttrList.push(SaleAttrValueList);
      this.attrId = "";
    },
    showInput(row) {
      this.$set(row, "inputVisible", true);
      this.$set(row, "inputValue", "");
    },
    handleInputConfirm(row) {
      row.inputVisible = false;
      const { baseSaleAttrId, inputValue } = row;
      if (inputValue.trim() == "") {
        this.$message("属性值不能为空");
        return;
      }
      let reslut = row.spuSaleAttrValueList.every((item) => {
        return item.saleAttrValueName != inputValue;
      });
      if (!reslut) {
        this.$message("请勿输入重复的属性值");
        return;
      }
      const newList = { baseSaleAttrId, saleAttrValueName: inputValue };
      row.spuSaleAttrValueList.push(newList);
    },
    async AddOrUpdateSpu() {
      //整理参数
      this.spu.spuImageList = this.ImageUrl.map((item) => {
        return {
          imgName: item.imgName,
          //如果item下有response和response.data这个数据，就将response.data给imgurl负责就将url给imgurl
          imgUrl: item.response ? item.response.data : item.url,
        };
      });
      const reslut = await this.$API.spu.reqAddOrUpdate(this.spu);
      if (reslut.code == 200 && this.spu.spuName != '') {
        this.$message({
          type: "success",
          message: "保存成功",
        });
        this.changeScene();
      }else {
         this.$message('请输入完整的信息')
       
      }
    },
    async addGetDate(category3Id) {
      this.spu.category3Id = category3Id
      let tradeMarkListReslut = await this.$API.spu.reqTrademarkList();
      if (tradeMarkListReslut.code == 200) {
        this.trademark = tradeMarkListReslut.data;
      }
        let SpuInfoList = await this.$API.spu.reqBaseAttrList();
      if (SpuInfoList.code == 200) {
        this.TypeList = SpuInfoList.data;
      }
    },
  },
  computed: {
    unselectArrList() {
      return this.TypeList.filter((item) => {
        return this.spu.spuSaleAttrList.every((item1) => {
          return item.name != item1.saleAttrName;
        });
      });
    },
  },
};
</script>
<style>
.el-tag + .el-tag {
  margin-left: 10px;
}
.button-new-tag {
  margin-left: 10px;
  height: 32px;
  line-height: 30px;
  padding-top: 0;
  padding-bottom: 0;
}
.input-new-tag {
  width: 90px;
  margin-left: 10px;
  vertical-align: bottom;
}
</style>
